using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MoveOfCamera : MonoBehaviour
{
    public GameObject Player;
    public float speed;
    public bool CanMove;

    public Vector2 minPosition;
    public Vector2 maxPosition;
    void Start()
    {
       GetPlayer();
    }

    // Update is called once per frame
    void LateUpdate()
    {
        moveToPlayer();
    }
    private void moveToPlayer()
    {
        if (CanMove&&Player!=null)
        {
            Vector3 targetPosition=Player.transform.position;
            targetPosition.x = Mathf.Clamp(targetPosition.x, minPosition.x, maxPosition.x);
            targetPosition.y = Mathf.Clamp(targetPosition.y, minPosition.y, maxPosition.y);
            transform.position = Vector3.Lerp(transform.position, targetPosition, speed);

        }
        
    }
    public void GetPlayer()
    {
        if (Player == null)
        {
            Player = GameObject.FindWithTag("Player");
        }
    }
    public void SetMoveRange(Vector2 min,Vector2 max)
    {
        minPosition = min;
        maxPosition = max;
        
    }
}
